﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Drawing;
using RA.Common;

//delegate void MyDel(string result);  

namespace RA.Services
{
    public class ImageProcessService
    {
       

        IAgent _imageProcessor;

        public ImageProcessService()
        {
            _imageProcessor = new ImageProcessor();//Settings.Default);
            _imageProcessor.Settings = Settings.Current; 
            
        }

        public void SetSettings(Settings settings)
        {
            Settings.Current = settings;
            _imageProcessor.Settings = settings;
        }

        public Settings GetSettings()
        {
            
            return _imageProcessor.Settings;
        }

        public void ChangeAgent(IAgent agent, Bitmap bitmap)
        {
            _imageProcessor = agent;//_compileService.GetAgent(fileName);
           //_imageProcessor.Settings = Settings.Default;
            if(bitmap !=null)
                _imageProcessor.ProcessImage(bitmap);
        }

        //public Bitmap GetContours(Bitmap image)
        //{
        //    //var frame = new Image<Bgr, byte>(image);

        //   // var bm  =_imageProcessor.c.ProcessImage(image); //TODO

        //    var bm = new Bitmap(image);

        //    using (Graphics g = Graphics.FromImage(bm))
        //    {
        //        foreach (var contour in _imageProcessor.Contours)
        //            if (contour.Total > 1)
        //            {
        //                g.DrawLines(new Pen(Color.Red), contour.ToArray());
        //            }

        //    } 
        //    return bm;
        //}

        public Templates GetTemplates(Bitmap bitmap)
        {
            _imageProcessor.ProcessImage(bitmap);
            GenerateSamplesNames(_imageProcessor.Samples);
            return _imageProcessor.Samples;
        }

        public Bitmap ShowTemplate(Bitmap bitmap, Template template)
        {
            var foundTemplate = _imageProcessor.Finder.FindTemplate(_imageProcessor.Samples, template);

            if (foundTemplate == null)
            {
                _imageProcessor.Settings.Blur = false;
                foundTemplate = _imageProcessor.Finder.FindTemplate(_imageProcessor.Samples, template);
                if(foundTemplate == null)
                    return bitmap;
            }
            var bm = new Bitmap(bitmap);
            using (Graphics g = Graphics.FromImage(bm))
            {
                DrawTemplate(bm, foundTemplate.template, g);
            }
            return bm;
        }

        public Bitmap ShowAllTemplates(Bitmap bitmap)
        { 
            var bm = new Bitmap(bitmap);
            using (Graphics g = Graphics.FromImage(bm))
            {
                foreach (var template in _imageProcessor.Samples)
                {
                    DrawTemplate(bm, template, g);
                }
            }
            return bm;
        }

        #region utils
        void GenerateSamplesNames(Templates templates)
        {
            int i = 0;
            foreach (var t in templates)
            {
                t.DisplayName = "Ш " + (++i).ToString();
            }
        }

        void DrawTemplate(Bitmap bitmap, Template template, Graphics graphics)
        {
            Rectangle foundRect = template.contour.SourceBoundingRect;
            Point p1 = new Point((foundRect.Left + foundRect.Right) / 2, foundRect.Top);
            string text = template.name;
            var font = new Font("Times New Roman", 12.0f);

            graphics.DrawRectangle(new Pen(Color.Blue), template.contour.SourceBoundingRect);
            graphics.DrawLines(new Pen(Color.Red), template.contour.GetPoints(template.startPoint));
            graphics.DrawString(template.DisplayName, font, new Pen(Color.Red).Brush,
                new PointF(p1.X - font.Height / 3, p1.Y - font.Height));
        }
        #endregion utils
    }
}
